//
//  GiftPickViewController.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/5.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "GiftPickViewController.h"
#import "SmallPickCollectionViewCell.h"
#import "NetworkHandler.h"
#import "UpCollectionViewCell.h"
#import "UpcellModel.h"
#import "BigPickCollectionViewCell.h"
#import "BigCellModel.h"
#import "HotDetailsViewController.h"
#import "TAMacro.h"

@interface GiftPickViewController ()
<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic,strong)UICollectionView * collectionViewSmall;
@property (nonatomic,strong)SmallPickCollectionViewCell * SmallCell;
@property (nonatomic,strong)NSMutableArray * namearray;
@property (nonatomic,strong)UIView * upView;
@property (nonatomic,strong)UICollectionView * UpcollectionView;
@property (nonatomic,strong)UpCollectionViewCell * UpCell;
@property (nonatomic,strong)UICollectionView * collectionViewBig;
@property (nonatomic,strong)BigPickCollectionViewCell * BigCell;

@property (nonatomic,strong)NSMutableArray * OneArray;
@property (nonatomic,strong)NSMutableArray * TwoArray;
@property (nonatomic,strong)NSMutableArray * ThreeArray;
@property (nonatomic,strong)NSMutableArray * FourArray;
@property (nonatomic,strong)NSMutableArray * array;
@property (nonatomic,strong)NSMutableArray * Bigarray;
@property (nonatomic,strong)NSMutableArray * arrayBig;
@property (nonatomic,assign)NSInteger indexNum;
@end

@implementation GiftPickViewController
-(NSMutableArray *)arrayBig{
    if (!_arrayBig) {
        self.arrayBig = [NSMutableArray array];
    }
    return _arrayBig;
}


-(NSMutableArray *)Bigarray{
    if (!_Bigarray) {
        self.Bigarray = [NSMutableArray array];
    }
    return _Bigarray;
}

- (NSMutableArray *)array{
    if (!_array) {
        self.array = [NSMutableArray array];
    }
    return _array;
}
-(NSMutableArray *)namearray{
    
    if (!_namearray) {
        self.namearray = [NSMutableArray array];
    }
    return _namearray;
    
}


-(NSMutableArray *)OneArray{
    if (!_OneArray) {
        self.OneArray = [NSMutableArray array];
    }
    return _OneArray;
}

-(NSMutableArray *)TwoArray{
    if (!_TwoArray) {
        self.TwoArray = [NSMutableArray array];
    }
    return _TwoArray;
}
-(NSMutableArray *)ThreeArray{
    if (!_ThreeArray) {
        self.ThreeArray = [NSMutableArray array];
    }
    return _ThreeArray;
}
-(NSMutableArray *)FourArray{
    if (!_FourArray) {
        self.FourArray = [NSMutableArray array];
    }
    return _FourArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.navigationController.navigationBar.translucent = NO;
    UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];
    [butt setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    butt.frame  = CGRectMake(0, 0, 50, 50);
    UIBarButtonItem *left= [[UIBarButtonItem alloc]initWithCustomView:butt];
    [butt addTarget:self action:@selector(clickLeft:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =left;
    self.prcie = @"";
    self.personality = @"";
    self.target = @"";
    self.scene = @"";
    self.view.backgroundColor =[UIColor brownColor];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.98 green:0.29 blue:0.31 alpha:1.00]];
    self.navigationController.navigationBar.translucent = NO;
    [self foundSmallCollectionView];
    [self foundBigCollectionView];
    [self BigAnalysis];
    
    [self foundView];
    
    
    
    
}

-(void)foundView{
    self.upView = [[UIView alloc]init];
    self.upView.backgroundColor = [UIColor whiteColor];
    self.upView.frame = CGRectMake(0, -SCREEN_WIDTH / 12 - SCREEN_HEIGHT / 4, SCREEN_WIDTH, SCREEN_HEIGHT / 4) ;
    [self foundUPCollectionView];
    [self.view addSubview:self.upView];
}
-(void)foundUPCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 4, SCREEN_WIDTH/10);
    flowLayout.minimumInteritemSpacing = 3;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 0, 0, 0); // 内边距
    self.UpcollectionView= [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,  SCREEN_HEIGHT / 4) collectionViewLayout:flowLayout
                            ];
    self.UpcollectionView.backgroundColor = [UIColor whiteColor];
    self.UpcollectionView.delegate = self;
    self.UpcollectionView.dataSource = self;
    [self.upView addSubview:_UpcollectionView];
    [self.UpcollectionView registerClass:[UpCollectionViewCell class] forCellWithReuseIdentifier:@"UpCell"];
    [self analysis];
}
-(void)foundBigCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH/2 - 15, SCREEN_HEIGHT /2.44);
    flowLayout.sectionInset = UIEdgeInsetsMake(0 , 5, 5, 5);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    self.collectionViewBig = [[UICollectionView alloc]initWithFrame:CGRectMake(0, SCREEN_WIDTH / 12, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
    self.collectionViewBig.delegate = self;
    self.collectionViewBig.dataSource = self;
    self.collectionViewBig.backgroundColor = [UIColor whiteColor];
    [self.collectionViewBig registerClass:[BigPickCollectionViewCell class] forCellWithReuseIdentifier:@"BigCell"];
    [self.view addSubview:self.collectionViewBig];
}
#pragma mark ---collectionViewSmall
-(void)foundSmallCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 5, SCREEN_WIDTH/12);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 15;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 0); // 内边距
    self.collectionViewSmall = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,  SCREEN_WIDTH/12) collectionViewLayout:flowLayout];
    self.collectionViewSmall.delegate = self;
    self.collectionViewSmall.dataSource = self;
    [self.view addSubview:_collectionViewSmall];
    [self.collectionViewSmall registerClass:[SmallPickCollectionViewCell class] forCellWithReuseIdentifier:@"SmallCell"];
}
-(void)BigAnalysis{
    self.prcie2 = self.prcie;
    self.personality2 = self.personality;
    self.target2 = self.target;
    self.scene2 = self.scene;
    [self.arrayBig removeAllObjects];
    NSString *urlString = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/search/item_by_type?price=%@&personality=%@&limit=20&offset=0&target=%@&scene=%@",self.prcie2,self.personality2,self.target2,self.scene2];
    [NetworkHandler getDataByURLString:urlString BodyString:nil WithDataBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *array = [dic objectForKey:@"items"];
        for (NSDictionary *dic in array) {
            BigCellModel *model = [BigCellModel modelWithDic:dic];
            [self.arrayBig addObject:model];
            //            [self.array addObject:model];
        }
        [self.collectionViewBig reloadData];
    }];
}
#pragma mark --- 解析最上方按钮
-(void)analysis{
    
    [NetworkHandler getDataByURLString:@"http://api.liwushuo.com/v2/search/item_filter" BodyString:nil WithDataBlock:^(id result) {
        
        
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"filters"];
        self.array = arr;
        for (NSDictionary *dic in arr) {
            [self.namearray addObject:[dic objectForKey:@"name"]];
            
        }
        [self getChannels];
        [self.collectionViewSmall reloadData];
        [self.UpcollectionView reloadData];
        
        
    }];
    
    
    
}



- (void)getChannels{
    
    
    UpcellModel *model = [[UpcellModel alloc]init];
    model.key = @"";
    
    
    model.Name  = @"全部";
    [self.OneArray addObject:model];
    [self.TwoArray addObject:model];
    [self.ThreeArray addObject:model];
    [self.FourArray addObject:model];
    
    for (NSDictionary *dic in [self.array[0] objectForKey:@"channels"]) {
        UpcellModel *model = [UpcellModel modelWithDic:dic];
        [self.OneArray addObject:model];
    }
    for (NSDictionary *dic in [self.array[1] objectForKey:@"channels"]) {
        UpcellModel *model = [UpcellModel modelWithDic:dic];
        
        [self.TwoArray addObject:model];
    }
    for (NSDictionary *dic in [self.array[2] objectForKey:@"channels"]) {
        UpcellModel *model = [UpcellModel modelWithDic:dic];
        [self.ThreeArray addObject:model];
    }
    for (NSDictionary *dic in [self.array[3] objectForKey:@"channels"]) {
        UpcellModel *model = [UpcellModel modelWithDic:dic];
        [self.FourArray addObject:model];
    }
    
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    
    if (collectionView == self.collectionViewSmall) {
        return self.namearray.count;
    }else if (collectionView == self.UpcollectionView){
        return self.Bigarray.count;
    }else{
        return self.arrayBig.count;
        
    }
    
    
    
    
    
}


-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (collectionView == self.collectionViewSmall) {
        
        self.SmallCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SmallCell" forIndexPath:indexPath];
        
        self.SmallCell.nameLable.text= self.namearray[indexPath.row];
        
        return self.SmallCell;
        
    }else if (collectionView == self.UpcollectionView){
        
        self.UpCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UpCell" forIndexPath:indexPath];
        self.UpCell.backgroundColor =  [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0];
        
        
        UpcellModel *model = self.Bigarray[indexPath.item];
        self.UpCell.upCellmodel = model;
        
        
        return self.UpCell;
        
    }else{
        
        self.BigCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BigCell" forIndexPath:indexPath];
        
        BigCellModel *model = self.arrayBig[indexPath.item];
        self.BigCell.bigCellModel = model;
        
        return self.BigCell;
        
        
        
    }
    
    
    
    
    
    
    
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    if (collectionView == self.collectionViewSmall) {
        
        self.indexNum = indexPath.item;
        
        [UIView animateWithDuration:1 animations:^{
            self.upView.frame = CGRectMake(0, SCREEN_WIDTH / 12, SCREEN_WIDTH, SCREEN_HEIGHT / 4) ;
            
        }];
        
        if (indexPath.row == 0) {
            self.Bigarray = self.OneArray;
            [self.UpcollectionView reloadData];
            
        }else if (indexPath.row == 1){
            self.Bigarray = self.TwoArray;
            [self.UpcollectionView reloadData];
            
        }else if (indexPath.row == 2){
            self.Bigarray = self.ThreeArray;
            [self.UpcollectionView reloadData];
            
        }else{
            self.Bigarray = self.FourArray;
            [self.UpcollectionView reloadData];
            
        }
    }else if (collectionView == self.UpcollectionView){
        
        [UIView animateWithDuration:1 animations:^{
            self.upView.frame = CGRectMake(0, -SCREEN_WIDTH / 12 - SCREEN_HEIGHT / 4, SCREEN_WIDTH, SCREEN_HEIGHT / 4) ;
        }];
        UpcellModel *model = self.Bigarray[indexPath.item];
        SmallPickCollectionViewCell *cell =(SmallPickCollectionViewCell *) [self.collectionViewSmall cellForItemAtIndexPath:[NSIndexPath    indexPathForItem:self.indexNum inSection:0]];
        cell.nameLable.text = model.Name;
        if (self.indexNum == 0) {
            self.target = model.key;
            [self BigAnalysis];
        }else if (self.indexNum == 1){
            self.scene = model.key;
            [self BigAnalysis];
            
        }else if (self.indexNum == 2){
            self.personality = model.key;
            [self BigAnalysis];
            
        }else{
            self.prcie = model.key;
            [self BigAnalysis];
        }
    }else{
        HotDetailsViewController *HotDetails = [[HotDetailsViewController alloc]init];
        // 属性传值第3步
        BigCellModel *model = self.arrayBig[indexPath.item];
        HotDetails.str = model.Id;
        [self.navigationController pushViewController:HotDetails animated:YES];
    }
}


-(void)clickLeft:(UIButton*)qwe{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}


-(void)viewWillDisappear:(BOOL)animated{
    // 必须
    self.navigationController.navigationBar.translucent = YES;
    
}

@end
